//版本更新
/*获取版本*/
function checkVersion() {
    var systemType = api.systemType;
    var appVersion = api.appVersion;
    api.ajax({
        url: baseUrl + '/user/get_version',
        method: 'get',
        data: {
			values: {
                system:systemType,
                version:appVersion,
            }, 
        },
        headers:{
            'content-type': 'application/json',
		}
    }, function (ret,err) {
        if(ret.code == 1 && ret.data.url){
            versionUpdating(ret.data.url,ret.data.desc) 
        }
    });
}

/*版本更新*/
function versionUpdating(url, content) {
    var systemType = api.systemType;
    var dialogBox = api.require('dialogBox');
    dialogBox.tips({
        rect: {
            w: 300,
        },
        texts: {
            title: '检测到版本更新',
            content: content
        },
        buttons: [{
            normal: 'widget://res/no.png',
            highlight: 'widget://res/no.png',
        }, {
            normal: 'widget://res/yes.png',
            highlight: 'widget://res/yes.png',

        }],
        tapClose: false,
        styles: {
            bg: '#FFF',
            topImage: {
                path: 'widget://res/gengxin.png',
                w: 300,
                h: 120
            },
            title: {
                color: '#000',
                size: 16,
                marginT: 10
            },
            content: {
                color: '#333',
                size: 12,
                marginT: 10
            },
            border: {
                color: '#eee',
                width: 2,
                marginT: 20
            },
            buttons: [{
                iconSize: 50,
                textColor: '#000',
                textSize: 0,
                marginT: 10,
                space: 100
            }, {
                iconSize: 50,
                textColor: '#000',
                textSize: 0,
                marginT: 10,
                space: 100
            }]
        }
    }, function (ret) {
        if (ret.index == 0) {
            api.closeWidget({
                id: 'A6157934774690',
                silent: true
            });
            dialogBox.close({
                dialogName: 'tips'
            });
        } else {
            dialogBox.close({
                dialogName: 'tips'
            });
            if (systemType == 'android') {
                versionUpdating_android(url);
            } else {
                versionUpdating_ios(url);
            }
        }
    });
}

/*android*/
function versionUpdating_android(url) {
    var UIActionProgress = api.require('UIActionProgress');
    UIActionProgress.open({
        maskBg: 'rgba(0,0,0,0.5)',
        styles: {
            h: 108,
            bg: '#fff',
            title: {
                size: 16,
                color: '#000',
                marginT: 10
            },
            msg: {
                size: 12,
                color: '#000',
                marginT: 5
            },
            lable: {
                size: 14,
                color: '#696969',
                marginB: 5
            },
            progressBar: {
                size: 3,
                normal: '#999',
                active: '#FF6189',
                marginB: 35,
                margin: 5
            }
        },
        data: {
            title: '正在更新',
            value: 0
        }
    }, function (ret) {
        if (ret.eventType == 'show') {
            api.download({
                url: url,
                savePath: 'fs://gongxiangyun.apk',
                report: true,
                cache: false,
                allowResume: true
            }, function (ret, err) {
                if (ret.state == 1) {
                    UIActionProgress.close();
                    api.installApp({
                        appUri: 'fs://gongxiangyun.apk'
                    });
                } else {
                    UIActionProgress.setData({
                        data: {
                            title: '正在更新...',
                            value: ret.percent
                        }
                    });
                }
            });
        }
    });
}

/*ios*/
function versionUpdating_ios(url) {
    var openAppStore = api.require('openAppStore');
    openAppStore.appStore({
        appID: url
    });
}

function endWith(string, str) {
    if (arguments.length != 2) {
    console.log("传入的参数数量不对。")
    }
    return string.slice(-str.length) == str;
}
// 如果有字母，那就把所有的字母转为.ascii
function formatString () {
    if (arguments.length != 1) {
        console.log("无字符串，无法格式化")
    }
    var reg = /([a-zA-Z])/g;
    var regarray = arguments[0].match(reg);
    if (regarray == null) {
    return arguments[0].split('.');
    }
    if (regarray  && regarray.length != 1) {
        console.log("格式错误，只允许出现一个字母")
    }
    var regString = regarray.join('');
    if (endWith(arguments[0], regString) != true) {
        console.log()
    throw "传入的版本号有错";
    }
    return arguments[0].replace(regString, "." + regString.charCodeAt()).split('.');
}

function myCompare() {
    if(arguments.length > 2) {
        console.log('目前只允许比较两个版本号')
    } else if (arguments.length < 2){
        console.log('比较版本必须传入参数')
    } else {
    if (arguments[0] == arguments[1]) {
        return 0;
    }
    var arr1 = formatString(arguments[0]);
    var arr2 = formatString(arguments[1]);
    var length = Math.min(arr1.length, arr2.length);
    for (var i = 0 ; i != length; i ++) {
        if (arr1[i] > arr2[i]) {
        return 1;
        } else if (arr1[i] < arr2[i]) {
        return -1;
        } else {

        };
    }
    if (length == arr1.length) {
        return -1;
    } else {
        return 1;
    }
    }
}